package com.suns.sharephoto.mapper;

import com.suns.sharephoto.entity.Photo;
import com.suns.sharephoto.vo.PhotoVO;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface PhotoMapper {
    // 获取轮播图（点赞数最多的5张图）
    List<Photo> findSlides();
    
    // 分页获取图片列表
    List<Photo> findByPage(int offset, int limit);
    
    // 获取图片总数
    int count();
    
    // 根据ID获取图片
    Photo findById(Integer id);
    
    // 更新点赞数
    void updateLikes(Integer id, Integer likes);
    
    // 插入新图片
    void insert(Photo photo);
    
    @Select("SELECT COUNT(*) FROM photos WHERE user_id = #{userId}")
    Integer countUserPhotos(Integer userId);
    
    @Select("SELECT COALESCE(SUM(likes), 0) FROM photos WHERE user_id = #{userId}")
    Integer countUserTotalLikes(Integer userId);
    
    @Select("SELECT p.*, u.nickname, u.avatar_url " +
            "FROM photos p " +
            "LEFT JOIN users u ON p.user_id = u.id " +
            "WHERE p.user_id = #{userId} " +
            "ORDER BY p.upload_time DESC " +
            "LIMIT #{offset}, #{size}")
    List<PhotoVO> getUserPhotos(@Param("userId") Integer userId,
                               @Param("offset") Integer offset,
                               @Param("size") Integer size);
    
    @Delete("DELETE FROM photos WHERE id = #{photoId} AND user_id = #{userId}")
    Integer deletePhoto(@Param("photoId") Integer photoId, @Param("userId") Integer userId);
} 